package com.example.dao.imp;

import com.example.dao.PostDao;
import com.example.domain.Post;
import com.example.utils.MyBatisUtil;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PostDaoImp implements PostDao {

    @Override
    public void addPost(Post post) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            sqlSession.insert("com.example.dao.PostDao.addPost", post);
            sqlSession.commit();
        }
    }

    @Override
    public void deletePost(Integer id) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            sqlSession.delete("com.example.dao.PostDao.deletePost", id);
            sqlSession.commit();
        }
    }

    @Override
    public List<Post> getAllPosts() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.PostDao.getAllPosts");
        }
    }

    @Override
    public List<Post> searchPostsByContent(String keyword) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.PostDao.searchPostsByContent", keyword);
        }
    }

    @Override
    public List<Post> getPostsByUserId(Integer userId) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.PostDao.getPostsByUserId", userId);
        }
    }

    @Override
    public List<Post> getPostsByPage(RowBounds rowBounds) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.PostDao.getPostsByPage", null, rowBounds);
        }
    }

    @Override
    public List<Post> getPostWithComments() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.PostDao.getPostWithComments");
        }
    }

    @Override
    public List<Post> getPostsByUserIdAndCategoryId(Integer userId, Integer categoryId) {
        Map<String, Integer> paramMap = new HashMap<>();
        paramMap.put("userId", userId);
        paramMap.put("categoryId", categoryId);
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.PostDao.getPostsByUserIdAndCategoryId", paramMap);
        }
    }

    public List<Post> getPostsByUserIdAndContent(Integer userId, String keyword) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("keyword", keyword);
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectList("com.example.dao.PostDao.getPostsByUserIdAndContent", params);
        }
    }

    @Override
    public Post getPostById(Integer postId) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            return sqlSession.selectOne("com.example.dao.PostDao.getPostById", postId);
        }
    }
}
